package com.banana.manager.forest.service.impl.user;

import com.banana.manager.forest.dao.userandinfo.IUserDao;
import com.banana.manager.forest.entity.user.UserBean;
import com.banana.manager.forest.service.iuser.IUserService;
import com.banana.manager.forest.util.PageInfoSize;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Resource
    private IUserDao userDao;

    @Override
    public UserBean userLogin(String username, String password) {
        QueryWrapper<UserBean> checkName = new QueryWrapper<>();
        checkName.eq("user_name",username);
        checkName.eq("user_password",password);
        return userDao.selectOne(checkName);
    }

    @Override
    public UserBean addUser(UserBean userBean) {
        userDao.insert(userBean);
        return userDao.selectById(userBean.getUserId());
    }

    @Override
    public void deleteUserById(Integer id) {
        userDao.deleteById(id);
    }

    @Override
    public UserBean updateUser(UserBean userBean) {
        userDao.updateById(userBean);
        return userDao.selectById(userBean.getUserId());
    }

    /**
     * 通过page和role返回一个pageinfo对象
     * @param page
     * @param role
     * @return
     */
    @Override
    public PageInfo<UserBean> selectUserByPage(Integer page, String role) {
        QueryWrapper<UserBean> pageCut = new QueryWrapper<>();
        pageCut.like("user_role",role);
        PageHelper.startPage(page, PageInfoSize.pageSize);
        PageInfo<UserBean> pageInfo = PageInfo.of(userDao.selectList(pageCut));
        return pageInfo;
    }

    @Override
    public UserBean findById(Integer id) {
        return userDao.selectById(id);
    }


}
